package com.hengpeng.itfinoss.service.repayment.impl;

import java.util.List;

import org.hibernate.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.util.DateTimeUtil;
import com.hengpeng.itfinbase.persist.ReceivePlan;
import com.hengpeng.itfinbase.persist.RecoverRepay;
import com.hengpeng.itfinbase.persist.RecoverRepay.RecoverRepayStatus;
import com.hengpeng.itfinbase.persist.RepaymentPlan;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.repayment.RepaymentService;

@Lazy
@Service
public class RepaymentServiceImpl implements RepaymentService
{
	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<List<RepaymentPlan>> getRepaymentPlanList(int currentPage, int pageSize, String status,
			String borrowUserName, String borrowInfoName, String beginDate, String endDate)
	{

		DynamicQuery query = new DynamicQuery(RepaymentPlan.class);
		if (status.equals(RepaymentPlan.ReplaymentStatus.UNSETTLE.toString()))
		{// 待还款
			query.eq("status", RepaymentPlan.ReplaymentStatus.UNSETTLE);// 待还款状态
			query.eq("isOverDured ", BoolValue.NO);// 还未逾期
		}
		else if (status.equals(RepaymentPlan.ReplaymentStatus.SETTLING.toString()))
		{// 还款中
			query.eq("status", RepaymentPlan.ReplaymentStatus.SETTLING);// 还款中状态
		}
		else if (status.equals(RepaymentPlan.ReplaymentStatus.REPAYMENTED.toString()))
		{// 已还清
			query.eq("status", RepaymentPlan.ReplaymentStatus.REPAYMENTED);
		}
		else if ("OVERDURED".equals(status))
		{// 已逾期
			query.eq("isOverDured ", BoolValue.YES);// 已经逾期
			query.eq("status", RepaymentPlan.ReplaymentStatus.UNSETTLE);// 还未还清
		}
		if (!StringHelper.isEmpty(borrowUserName))
		{
			query.eq("borrowUserName", borrowUserName);
		}
		if (!StringHelper.isEmpty(borrowInfoName))
		{
			query.like("borrowName", "%" + borrowInfoName + "%");
		}
		if (!StringHelper.isEmpty(beginDate))
		{
			query.ge("lastRepayDate", DateTimeUtil.formatToDate(beginDate, DateTimeUtil.DATE_FORMAT_YMD));
		}
		if (!StringHelper.isEmpty(endDate))
		{
			query.le("lastRepayDate", DateTimeUtil.formatToDate(endDate, DateTimeUtil.DATE_FORMAT_YMD));
		}
		query.setPageIndex(currentPage);
		query.setPageSize(pageSize);
		query.desc("borrowInfoId");
		query.asc("curRepayNum");
		query.setQueryType(QueryType.JDBC);
		query.setIsSearchTotalCount(true);

		QueryResult<RepaymentPlan> requery = serviceFactory.getPersistService()
				.findWithPage(RepaymentPlan.class, query);
		Long count = requery.getTotalCount();
		ServiceResult<List<RepaymentPlan>> serviceResult = new ServiceResult<List<RepaymentPlan>>();// 设置返回值
		serviceResult.setReturnValue(requery.getResults());
		serviceResult.setReturnListSize(count.intValue());
		return serviceResult;
	}

	@Override
	public ServiceResult<List<RecoverRepay>> getRecoverRepayList(int currentPage, int pageSize,
			RecoverRepay.RecoverRepayStatus status, String borrowUserName)
	{
		DynamicQuery query = new DynamicQuery(RecoverRepay.class);
		query.eq("status", status);
		if (!StringHelper.isEmpty(borrowUserName))
		{
			query.eq("borrowUserName", borrowUserName);
		}
		query.setPageIndex(currentPage);
		query.setPageSize(pageSize);
		query.desc("borrowInfoId");
		query.asc("curRepayNum");
		query.setQueryType(QueryType.JDBC);
		query.setIsSearchTotalCount(true);

		QueryResult<RecoverRepay> requery = serviceFactory.getPersistService().findWithPage(RecoverRepay.class, query);
		Long count = requery.getTotalCount();
		ServiceResult<List<RecoverRepay>> serviceResult = new ServiceResult<List<RecoverRepay>>();// 设置返回值
		serviceResult.setReturnValue(requery.getResults());
		serviceResult.setReturnListSize(count.intValue());
		return serviceResult;
	}

	@Override
	public List<ReceivePlan> getReceivePlans(Long borrowinfoId, Integer curRecNum, ReceivePlan.ReceiveStatus[] status)
	{
		DynamicQuery query = new DynamicQuery(ReceivePlan.class);
		if (borrowinfoId != null)
		{
			query.eq("borrowinfoId", borrowinfoId);
		}
		if (curRecNum != null)
		{
			query.eq("curRecNum", curRecNum);
		}
		if (status != null && status.length > 0)
		{
			query.inArray("status", status);
		}
		return serviceFactory.getPersistService().find(ReceivePlan.class, query);
	}

	@Override
	public RepaymentPlan getUnRepaymentPlan(Long repaymentPlanId)
	{
		DynamicQuery query = new DynamicQuery(RepaymentPlan.class);
		query.eq("id", repaymentPlanId);
		query.in("status", RepaymentPlan.ReplaymentStatus.UNSETTLE, RepaymentPlan.ReplaymentStatus.SETTLING);
		List<RepaymentPlan> list = serviceFactory.getPersistService().find(RepaymentPlan.class, query);
		return list == null || list.size() < 1 ? null : list.get(0);
	}

	@Override
	public RecoverRepay getRecoverRepay(Long id, RecoverRepayStatus[] status)
	{
		DynamicQuery query = new DynamicQuery(RecoverRepay.class);
		query.eq("id", id);
		if (status != null && status.length > 0)
		{
			query.inArray("status", status);
		}
		List<RecoverRepay> list = serviceFactory.getPersistService().find(RecoverRepay.class, query);
		return list == null || list.size() < 1 ? null : list.get(0);
	}
}
